// import Util from '../common-component/util/util.js';
// $(".login-page").height($(window).height());
$(".login-page").css({minHeight:$(window).height()})
var dotPro = $('#lgoin-submit').easyform(),user,password,userName = $("#lgoin-user"),psd = $("#lgoin-psd");
var tip = userName.easytip();
var psdTip = psd.easytip();
var flag = false;
function addCookie( objName, objValue, objDays, objDomain ){
    // use escape to avoid @#$%^%
    var str = objName + "=" + escape(objValue);
    str += ";path=/;domain=" + objDomain ;
    if( objDays > 0 ){
        var date = new Date();
        var ms = objDays * ( 3600 * 24 * 1000 );
        date.setTime( date.getTime() + ms );
        str += ";expires=" + date.toGMTString();
    }
    return document.cookie = str ;
}
/*
 * 登录
 * */
$(function(){
    document.onkeydown = function(e){
        var ev = document.all ? window.event : e;
        if(ev.keyCode==13) {
            if(!userName.val()){
                tip.show("请输入用户名")
                return
            }
            if(!psd.val()){
                psdTip.show("请输入密码")
                return
            }
            login()
        }
    }
    $(".js-login-ipt").on('keyup', function(e) {
        e.stopPropagation();
        checkLoginBtn();
    });
    setTimeout(function(){checkLoginBtn(true)}, 500);
});
function checkLoginBtn(auto) {
    var user = $('#lgoin-user').val(),
        password = $('#lgoin-psd').val(), reg = /^\s*$/;

    if(!reg.test(user) && !reg.test(password) || auto && !reg.test(user)) {
        flag = true;
        $("#login-button").addClass("primary");
    }else {
        flag = false;
        $("#login-button").removeClass("primary");
    }
}
//primary
function login() {
    dotPro.success = function (ef){//验证成功
        user = ef.inputs[0].value;
        password = ef.inputs[1].value;
    }
    user = $('#lgoin-user').val();
    password = $('#lgoin-psd').val();
    if(!user || !password || !flag) {
        return null
    }
    $("#login-button").val('登录中...');
    $.ajax({
        url: "http://47.96.24.204:8080/login",
        type:'post',
        data:{userName:user,password:password},
        success: function(json){
            if(json.success){
                addCookie( 'token' , json.data.token ,7 ,document.domain );
                // addCookie( 'branchCode' , json.result.branchCode || '',7 ,document.domain );
                // addCookie( 'stationCode' , json.result.manager.stationCode || '' ,7 ,document.domain );
                // addCookie( 'stationLevel' , json.result.stationLevel || '' ,7 ,document.domain );
                // addCookie( 'username' , json.result.manager.name || '' ,7 ,document.domain );
                window.location.href = '/#/home-page'
            }else{
                tip.show(json.error);
            }
        },
        complete: function() {
            $("#login-button").val('登录');
        }
    });
}
